<template>
  <ui-page ref="dataList" class="mine" :bg-color="true" @refresh="handlePageRefresh">
    <!-- 用户信息卡片 -->
    <view class="mine__profile" @click="handleProfileClick">
      <view class="mine__profile-avatar">
        <ui-avatar :src="userInfo.avatar" size="large"></ui-avatar>
      </view>
      <view class="mine__profile-main">
        <view class="mine__profile-name">{{ userInfo.nickname }}</view>
        <view class="mine__profile-tips">{{ translate('mine_profile_tips') }}</view>
      </view>
    </view>

    <!-- 菜单列表 -->
    <view class="mine__menu">
      <ui-form>
<!--
        <bi-mine-menu-item
            :icon="milesIcon"
            :title="translate('vehicle-mineMileage')"
            :desc="translate('vehicle-mineMileage-desc')"
            @click="handleMyMileageClick"
        />-->
        <bi-mine-menu-item
            :icon="deliveryCarImage"
            :title="translate('mine_delivery_car_title')"
            :desc="translate('mine_delivery_car_desc')"
            @click="handleMyDeliveryClick"
        />

        <bi-mine-menu-item
            v-if="showShenhe"
            :icon="indexShenheImage"
            :title="translate('mine_pending_review_title')"
            :desc="translate('mine_pending_review_desc')"
            @click="handleMyShenheClick"
        />

        <bi-mine-menu-item
            :icon="indexShoucangImage"
            :title="translate('mine_memo_title')"
            :desc="translate('mine_memo_desc')"
            @click="handleMyVehicleClick"
        />

        <bi-mine-menu-item
            :icon="indexWareHouseImage"
            :title="translate('mine_rental_title')"
            :desc="translate('mine_rental_desc')"
            @click="handleWareHouseClick"
        />

        <bi-mine-menu-item
            :icon="indexMyaoqingImage"
            :title="translate('mine_personnel_title')"
            :desc="translate('mine_personnel_desc')"
            @click="handlePeopleManageClick"
        />

        <bi-mine-menu-item
            :icon="indexMapImage"
            :title="translate('mine_vehicle_title')"
            :desc="translate('mine_vehicle_desc')"
            @click="handleCarManageClick"
        />

        <bi-mine-menu-item
            :icon="indexClockImage"
            :title="translate('mine_time_display_title')"
            @click="handleClockSettingClick"
        />

        <bi-mine-menu-item
            :icon="indexShezhimage"
            :title="translate('mine_settings_title')"
            @click="handleProfileClick"
        />

      </ui-form>
    </view>

    <!-- 登出按钮 -->
    <view class="mine__actions">
      <ui-button block type="primary" @click="handleLogoutClick">{{ translate('mine_logout') }}</ui-button>
    </view>

    <!-- 关注公众号提示 -->
    <view class="mine__tips" v-if="!hideTips">
      <bi-follow-official-account v-if="!checkFlag" @close="handleCloseTips"></bi-follow-official-account>
    </view>

    <!-- 客服弹窗 -->
    <ui-popup v-model="showServicePopup" type="center">
      <mine-service @confirm="handleConfirmClick"></mine-service>
    </ui-popup>

  </ui-page>
</template>

<script>
import BiMineMenuItem from '@/common/mine-menu-item/mine-menu-item.vue';
import { mapGetters, mapActions } from 'vuex';
import MineService from './mine-service.vue';
import BiFollowOfficialAccount from '@/common/follow-official-account/follow-official-account.vue';
import { getShareData } from '@/utils/share.js';
import { getLoginUser } from '@/apis/account.js';

// 导入语言文件
import zhLang from '@/utils/zh.js';
import enLang from '@/utils/en.js';
import storage from "@huntianning/miniprogram/utils/storage";

export default {
  components: {
    MineService,
    BiFollowOfficialAccount,
    BiMineMenuItem
  },
  data() {
    return {
      userInfo:{
        avatar: '',
        nickname: '',
        id: ''
      },
      kefuVisible:false,
      indexShenheImage: 'https://hsmw-tool-pic-1303885219.cos.ap-beijing.myqcloud.com/weapp/files/htn/shenhe.png',
      indexMapImage: 'https://hsmw-tool-pic-1303885219.cos.ap-beijing.myqcloud.com/weapp/files/htn/car.png',
      indexShoucangImage: 'https://hsmw-tool-pic-1303885219.cos.ap-beijing.myqcloud.com/weapp/files/htn/my-shoucang.png',
      indexliulanjiluImage: 'https://hsmw-tool-pic-1303885219.cos.ap-beijing.myqcloud.com/weapp/files/htn/liulanjilu.png',
      indexYaoqingImage: 'https://hsmw-tool-pic-1303885219.cos.ap-beijing.myqcloud.com/weapp/files/htn/yaoqing.png',
      indexMyaoqingImage: 'https://hsmw-tool-pic-1303885219.cos.ap-beijing.myqcloud.com/weapp/files/htn/people.png',
      indexKefuImage: 'https://hsmw-tool-pic-1303885219.cos.ap-beijing.myqcloud.com/weapp/files/htn/kefu.png',
      indexXieyimage: 'https://hsmw-tool-pic-1303885219.cos.ap-beijing.myqcloud.com/weapp/files/htn/xieyi.png',
      indexShezhimage: 'https://hsmw-tool-pic-1303885219.cos.ap-beijing.myqcloud.com/weapp/files/htn/setup.png',
      indexClockImage: 'https://hsmw-tool-pic-1303885219.cos.ap-beijing.myqcloud.com/weapp/files/htn/display.png',
      indexWareHouseImage:'https://hsmw-tool-pic-1303885219.cos.ap-beijing.myqcloud.com/weapp/files/htn/warehouse.png',
      deliveryCarImage:'https://hsmw-tool-pic-1303885219.cos.ap-beijing.myqcloud.com/weapp/files/htn/delivery-car.png',
      deliveryOrderImage:'https://hsmw-tool-pic-1303885219.cos.ap-beijing.myqcloud.com/weapp/files/htn/delivery-order.png',
      milesIcon: 'https://aiduk-1334050802.cos.ap-guangzhou.myqcloud.com/ztc/icons/miles.png',
      analytics:"https://aiduk-1334050802.cos.ap-guangzhou.myqcloud.com/ztc/icons/analisys.png",
      checkFlag:true,
      hideTips: false,
      showServicePopup: false,
      isType:null,
      currentLocale: 'zh', // 默认语言
      // 语言包
      languages: {
        'zh': zhLang,
        'en': enLang
      }
    };
  },
  computed: {
    ...mapGetters(['currentUser']),
    ...mapGetters(['isLogin']),
    showShenhe() {
      return true;
    }
  },
  onShareAppMessage() {
    return getShareData();
  },
  onShow() {
    this.getUserInfo();
  },
  onLoad() {
    // 初始化语言
    let savedLanguage = uni.getStorageSync('language') || 'zh';
    const currentTimeType = storage.get('CURRENT_TIME_TYPE');
    savedLanguage=(currentTimeType === 1 || currentTimeType === '1') ? 'en' : 'zh';
    this.currentLocale = savedLanguage;

    if (!this.isLogin) {
      uni.redirectTo({
        url: '/pages/login/login?returnUrl='+'\/pages\/mine\/mine'
      });
      setTimeout(() => {
        uni.showToast({
          icon: 'none',
          title: '您当前未登录，请登录后再查看我的信息,已为您跳转到登录页面！',
          slideDuration:1000
        });
      }, 1500);
    }else {
      getLoginUser().then((res)=>{
        this.isType=res.type;
        if(res){
          this.userInfo.avatar=res.avatar;
          this.userInfo.nickName=res.nickName;
        }
      });
    }
  },
  methods: {
    // 翻译函数 - 与 order-delivery 页面保持一致
    translate(key) {
      try {
        const langPack = this.languages[this.currentLocale] || this.languages['zh'];
        return langPack[key] || key;
      } catch (e) {
        console.error('Translation error:', e);
        return key;
      }
    },

    handlePageRefresh(){
      this.getUserInfo();
    },
     getUserInfo(){
      getLoginUser().then(res => {
        if(res){
          this.userInfo.avatar=res.avatar;
          this.userInfo.nickname=res.nickname;
          this.userInfo.id=res.id;
        }
      });
    },
    handleShowCode() {
      this.showServicePopup = true;
    },
    ...mapActions(['SetUserLogout']),
    handleCloseTips() {
      this.hideTips = true;
    },
    handleServiceClick() {
      this.showServicePopup = true;
    },
    handleMyMileageClick() {
      uni.navigateTo({
        url: '/packages/mine/mine-mileage/mine-mileage'
      });
    },
    handleMyDeliveryClick() {
      uni.navigateTo({
        url: '/packages/mine/mine-delivery-car/mine-delivery-car'
      });
    },

    handleMyShenheClick() {
      uni.navigateTo({
        url: '/packages/mine/mine-shenhe/mine-shenhe'
      });
    },
    handleMyVehicleClick() {
      uni.navigateTo({
        url: '/packages/order/order-my/order-my'
      });
    },
    handleWareHouseClick() {
      uni.navigateTo({
        url: '/packages/mine/mine-cangku/mine-cangku'
      });
    },
    handleScVehicleClick() {
      uni.navigateTo({
        url: '/pages/vehicle-sc/vehicle-sc'
      });
    },
    handleSeeVehicleClick() {
      uni.navigateTo({
        url: '/pages/vehicle-see/vehicle-see'
      });
    },
    handleShenheVehicleClick() {
      uni.navigateTo({
        url: '/packages/mine/sell/sell-shenhe'
      });
    },
    handleClockSettingClick() {
      uni.navigateTo({
        url: '/packages/mine/mine-setting/mine-setting-clock'
      });
    },
    handleYinsiClick() {
      uni.navigateTo({
        url: '/packages/agreement/agreement-privacy/agreement-privacy'
      });
    },
    handleInviteClick() {
      uni.navigateTo({
        url: '/pages/invite/invite'
      });
    },
    handleProfileClick() {
      uni.navigateTo({
        url: '/packages/mine/mine-setting/mine-setting'
      });
    },
    handleDataCountClick() {
      uni.navigateTo({
        url: '/packages/mine/mine-data/mine-data'
      });
    },
    handleCarManageClick() {
      uni.navigateTo({
        url: '/packages/mine/mine-car/mine-car'
      });
    },
    handlePeopleManageClick() {
      uni.navigateTo({
        url: '/packages/mine/mine-people/mine-people'
      });
    },
    handleBelongCompanyClick() {
      uni.navigateTo({
        url: '/packages/mine/mine-company/mine-company'
      });
    },
    handleprojManageClick() {
      uni.navigateTo({
        url: '/packages/project/project/project'
      });
    },
    handleCompanyClick() {
      uni.navigateTo({
        url: '/packages/common/involved-select/mine-involved'
      });
    },
    handleConfirmClick() {
      this.showServicePopup = false;
    },
    handleLogoutClick() {
      return this.SetUserLogout().then(() => {
        uni.reLaunch({
          url: '/pages/home/home'
        });
      });
    },

    // 切换语言的方法
    switchLanguage() {
      this.currentLocale = this.currentLocale === 'zh' ? 'en' : 'zh';
      uni.setStorageSync('language', this.currentLocale);
      this.$forceUpdate();
    }
  }
};
</script>

<style lang="less">
.mine {
  position: relative;
}
.mine__profile {
  display: flex;
  flex-direction: row;
  background-color: #fff;
  padding: 16px 36px 16px 16px;
  line-height: 1.7em;
  position: relative;
  &:active {
    background-color: rgba(0, 0, 0, 0.06);
  }
  &:after {
    content: '';
    position: absolute;
    top: 50%;
    right: 16px;
    transform: translateY(-50%) rotate(45deg);
    border: 1px solid #ddd;
    width: 8px;
    height: 8px;
    border-left: 0;
    border-bottom: 0;
  }
}
.mine__profile-avatar {
  margin-right: 16px;
}
.mine__profile-main {
  padding: 4px 0;
}
.mine__profile-name {
  font-size: 16px;
  font-weight: bold;
  margin-bottom: 2px;
}
.mine__profile-tips {
  color: #666666;
  font-size: 12px;
}
.mine__tips {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 8px 16px;
}
.mine__menu {
  margin-top: 16px;
  padding: 0 20rpx;
}
.mine__actions {
  padding: 16px;
}
.bi-mine-menu-item:active {
  background-color: #f9f9f9;
}
</style>
